package com.yq.homework.day281;


import java.io.FileWriter;
import java.io.IOException;

import java.util.Random;

/**
 * @author 3307235355@qq.com
 * @since 2022/06/28 20:09
 * <p>
 * (选做) 破解密码 要求: 从0-9这10个数中 - 生成一个长度是3的随机数字，把这个字符串当作密码
 * 创建一个破解线程，使用穷举法，匹配这个密码,直到匹配到正确的,匹配的可能的密码放入数组中
 * <p>
 * 创建一个日志线程，将用过的字符串(数组中的)写入文件，
 * 这个日志线程设计为守护线程
 * 注意: 守护线程的特征,jvm中只有守护线程 jvm退出
 * 所以要保证数组填充完了之后(破解线程先执行),再去写入
 */
public class Demo03 {
    public static void main(String[] args) throws InterruptedException {
        //随机密码的生成
        String password = getPassword();

        // 破解线程，进行密码的破解，然后存入数据到数组当中去
        Crack c1 = new Crack(password);
        Thread t1 = new Thread(c1, "破解线程正在执行");
        t1.start();
        t1.join();
        // System.out.println(Arrays.toString(Crack.s2));

        // 日志线程
        Log log = new Log(Crack.s2);
        Thread t2 = new Thread(log, "日志线程");
        t2.setDaemon(true);
        t2.start();

        Thread.sleep(3000);
    }

    //生成随机的密码
    public static String getPassword() {
        String s = "0123456789";
        char[] chars = s.toCharArray();
        String s1 = "";
        Random random = new Random();
        for (int i = 0; i < 3; i++) {
            int i1 = random.nextInt(10);
            s1 += chars[i1];
        }
        return s1;
    }
}


class Crack implements Runnable {
    String[] s1 = new String[1];
    static String[] s2 = new String[999];
    String password;
    static boolean flag = true;
    static int count = 0;

    public Crack(String password) {
        this.password = password;
    }

    @Override
    public void run() {


        System.out.println(Thread.currentThread().getName() + "正在执行");

        while (flag){
            for (int i = 0; i < 1000; i++) {
                //  穷举法的值
                String format = String.format("%03d", i);
                // 判断
                if (password.equals(format)) {
                    s1[0] = format;
                    flag=false;
                    break;
                } else {
                    s2[count++] = format;
                }
            }
        }
        System.out.println("破解出来的密码是："+s1[0]);


    }
}

class Log implements Runnable {
    String[] s2;

    public Log(String[] s2) {
        this.s2 = s2;
    }
    @Override
    public void run() {
        System.out.println(Thread.currentThread().getName() + "正在执行");
        FileWriter out = null;
        try {
            out = new FileWriter("log.txt");
            for (int i = 0; i < s2.length; i++) {
                if (s2[i]==null){
                    continue;
                }
                String s = s2[i];
                out.write(s+" ");
                if ( (i+1)%50==0){
                    out.write("\r\n");
                }
            }
            System.out.println("已经校验的密码已经写入log.txt");
        } catch (IOException e) {
            e.printStackTrace();
        }finally {
            if (out!=null){
                try {
                    out.close();
                } catch (IOException e) {
                    e.printStackTrace();
                }
            }
        }
    }
}
